package cn.qn.service.impl.security;

import cn.qn.base.PageVO;
import cn.qn.dto.security.RoleDTO;
import cn.qn.mapper.security.SecurityMapper;
import cn.qn.moudle.security.Menu;
import cn.qn.moudle.security.Role;
import cn.qn.moudle.security.RoleMenu;
import cn.qn.repository.security.MenuRepository;
import cn.qn.repository.security.RoleMenuRepository;
import cn.qn.repository.security.RoleRepository;
import cn.qn.service.security.RoleService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author: Bryant
 * @date: 2019/5/24
 * @description:
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {
    private Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleRepository roleRepository;
    @Resource
    private SecurityMapper securityMapper;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private RoleMenuRepository roleMenuRepository;

    @Override
    public List<Role> findByUserId(String userId) {
        return roleRepository.findByUserId(userId);
    }

    @Override
    public PageVO<Role> page(RoleDTO dto, PageBounds pageBounds) {
        List<Role> list = securityMapper.rolePage(dto,pageBounds);
        Integer count = securityMapper.rolePageCount(dto);
        return PageVO.convert(pageBounds.getPage(),pageBounds.getLimit(),list,count);
    }

    @Override
    public List<Role> findAllRole() {
        return roleRepository.findAll();
    }

    @Override
    public Role insert(Role role) {
        return roleRepository.save(role);
    }

    @Override
    public Role findByRoleCode(String code) {
        return roleRepository.findByCode(code);
    }

    @Override
    public Role update(Role role) {
        return roleRepository.save(role);
    }

    @Override
    public Set<Menu> menuPage(Integer id) {
        List<RoleMenu> roleMenu = roleMenuRepository.findByRoleId(id);
        Set<Menu> menus = new HashSet<>();
        if(null!=roleMenu && !roleMenu.isEmpty()){
            roleMenu.stream().forEach(r->{
                Optional<Menu> menu = menuRepository.findById(r.getMenuId());
                Menu m = menu.get();
                if(m.getParentId()!=0){
                    menus.add(m);
                }else{
                    if(m.getPriority().equals(0)){
                        menus.add(m);
                    }
                }
            });
        }
        return menus;
    }

    @Override
    public boolean menuAdd(Integer roleId, List<Integer> menuIds) {
        try{
            List<RoleMenu> roleMenuRelationships = roleMenuRepository.findByRoleId(roleId);
            roleMenuRepository.deleteAll(roleMenuRelationships);
            if(null!=menuIds && !menuIds.isEmpty()){
                menuIds.stream().forEach(m->{
                    RoleMenu relationship = new RoleMenu();
                    relationship.setMenuId(m);
                    relationship.setRoleId(roleId);
                    roleMenuRepository.save(relationship);
                });
            }
            logger.error("新增角色菜单成功");
            return true;
        }catch(Exception e){
            logger.error("新增角色菜单失败:"+e);
            return false;
        }
    }
}
